package com.yeyks.service.impl;

import com.yeyks.dal.dao.MessageMapper;
import com.yeyks.dal.domain.HotelInfo;
import com.yeyks.dal.domain.Message;
import com.yeyks.model.MessageConst;
import com.yeyks.service.MessageService;
import com.yeyks.util.Utility;
import com.yeyks.model.MessageConst;
import com.yeyks.service.MessageService;
import com.yeyks.util.Utility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MessageServiceImpl implements MessageService {
    private Logger logger = LoggerFactory.getLogger(MessageServiceImpl.class);

    @Autowired
    MessageMapper messageMapper;

    public void passHotelInfo(HotelInfo hotelInfo) {
        try {
            Message msg = new Message();
            msg.setTitle(MessageConst.ENROLL_HOTEL);
            msg.setContent(String.format(MessageConst.ENROLL_HOTEL_CONTENT, hotelInfo.getName()));
            msg.setType(MessageConst.MT_ENROLL_HOTEL);
            msg.setToUserType("AB");
            if (messageMapper.insertReturnId(msg) == 0 || msg.getId() == 0) {
                throw new Exception("写入数据库异常");
            }
        } catch (Throwable e) {
            logger.error(e.getMessage(), e);
        }

        try {
            Message msg = new Message();
            msg.setTitle(MessageConst.PASS_HOTEL_USER);
            msg.setContent(String.format(MessageConst.PASS_HOTEL_USER_CONTENT, hotelInfo.getName()));
            msg.setType(MessageConst.MT_PASS_HOTEL_USER);
            msg.setToUserId(hotelInfo.getAdminId());
            if (messageMapper.insertReturnId(msg) == 0 || msg.getId() == 0) {
                throw new Exception("写入数据库异常");
            }
        } catch (Throwable e) {
            logger.error(e.getMessage(), e);
        }
    }

    public void nopassHotelInfo(HotelInfo hotelInfo) {
        try {
            Message msg = new Message();
            msg.setTitle(MessageConst.NOPASS_HOTEL_USER);
            msg.setContent(String.format(MessageConst.NOPASS_HOTEL_USER_CONTENT,
                    hotelInfo.getName(), Utility.getExtraInfo(hotelInfo.getExtraInfo(), "nopass_comment")));
            msg.setType(MessageConst.MT_NOPASS_HOTEL_USER);
            msg.setToUserId(hotelInfo.getAdminId());
            if (messageMapper.insertReturnId(msg) == 0 || msg.getId() == 0) {
                throw new Exception("写入数据库异常");
            }
        } catch (Throwable e) {
            logger.error(e.getMessage(), e);
        }
    }

    public void rejectRefund(Integer userId) {
        try {
            Message msg = new Message();
            msg.setTitle(MessageConst.REJECT_REFUND);
            msg.setContent(MessageConst.REJECT_REFUND_CONTENT);
            msg.setType(MessageConst.MT_NOPASS_HOTEL_USER);
            msg.setToUserId(userId);
            if (messageMapper.insertReturnId(msg) == 0 || msg.getId() == 0) {
                throw new Exception("写入数据库异常");
            }
        } catch (Throwable e) {
            logger.error(e.getMessage(), e);
        }
    }

    public void msgToUser(Integer userId, String title, String content) {
        try {
            Message msg = new Message();
            msg.setTitle(title);
            msg.setContent(content);
            msg.setType(0);
            msg.setToUserId(userId);
            if (messageMapper.insertReturnId(msg) == 0 || msg.getId() == 0) {
                throw new Exception("写入数据库异常");
            }
        } catch (Throwable e) {
            logger.error(e.getMessage(), e);
        }
    }

    public void createAccount(Integer userId) {
        try {
            Message msg = new Message();
            msg.setTitle(MessageConst.CREATE_ACCOUNT);
            msg.setContent(MessageConst.CREATE_ACCOUNT_CONTENT);
            msg.setType(MessageConst.MT_CREATE_ACCOUNT);
            msg.setToUserId(userId);
            if (messageMapper.insertReturnId(msg) == 0 || msg.getId() == 0) {
                throw new Exception("写入数据库异常");
            }
        } catch (Throwable e) {
            logger.error(e.getMessage(), e);
        }
    }
}
